package com.tekj.cxqcb.view.Module.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.config.enums.EventBusMsgBean;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.PubStatusBean;
import com.tekj.cxqcb.operation.resultBean.SelectUserBankListBean;
import com.tekj.cxqcb.view.Module.adapter.BankCardListAdapter;
import com.tekj.cxqcb.view.Module.decoration.SpacesItemDecoration;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.Tools;
import es.dmoral.toasty.Toasty;

import static android.support.v7.widget.ListPopupWindow.MATCH_PARENT;

/**
 * 我的银行卡列表
 */
public class BankCardListActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.rv_car)
    SwipeRecyclerView rvCar;
    @BindView(R.id.srl_main)
    SwipeRefreshLayout srlMain;

    BusinessDao mod5Dao;
    BankCardListAdapter carListAdapter;

    //    List<SelectMyCarBean.DataBean.ListBean> mData;
    int page = 1;

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void init() {
        EventBus.getDefault().register(this);
        tvTitle.setText("银行卡管理");
        mod5Dao = new BusinessDao(this, this);
        mod5Dao.SelectUserBankList(MainBActivity.User.getUserId(), page);
        ivR.setImageResource(R.mipmap.icon_add);
        InitAdapter();

    }


    void InitAdapter() {
        rvCar.setLayoutManager(new LinearLayoutManager(activity));

        SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
                SwipeMenuItem deleteItem1 = new SwipeMenuItem(activity)
                        .setBackgroundColor(getResources().getColor(R.color.colorPrimary))
                        .setText("设置\n默认").setTextSize(13)
                        .setHeight(MATCH_PARENT)
                        .setWidth(Tools.dip2Px(activity, 60))
                        .setTextColor(getResources().getColor(R.color.white));
                rightMenu.addMenuItem(deleteItem1);

//                SwipeMenuItem deleteItem2 = new SwipeMenuItem(activity)
//                        .setBackgroundColor(getResources().getColor(R.color.color_CCCCCC))
//                        .setText("编辑").setTextSize(13)
//                        .setHeight(Util.dip2Px(activity, 80)
//                        ).setWidth(Util.dip2Px(activity, 40))
//                        .setTextColor(getResources().getColor(R.color.white));
//                rightMenu.addMenuItem(deleteItem2);

                SwipeMenuItem deleteItem3 = new SwipeMenuItem(activity)
                        .setBackgroundColor(getResources().getColor(R.color.red))
                        .setText("删除").setTextSize(13)
                        .setHeight(MATCH_PARENT)
                        .setWidth(Tools.dip2Px(activity, 60))
                        .setTextColor(getResources().getColor(R.color.white));
                rightMenu.addMenuItem(deleteItem3);
                // 注意：哪边不想要菜单，那么不要添加即可。
            }
        };
        // 设置监听器。
        rvCar.setSwipeMenuCreator(mSwipeMenuCreator);

        // 菜单点击监听。
        OnItemMenuClickListener mItemMenuClickListener = new OnItemMenuClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, final int position) {
                // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
                menuBridge.closeMenu();

                // 左侧还是右侧菜单：
                int direction = menuBridge.getDirection();
                if (direction == -1) {
                    // 菜单在Item中的Position：
                    switch (menuBridge.getPosition()) {
                        case 0:
                            Tools.showNormalDialogOne(activity, "默认设置", "是否将该卡设为默认"
                                    , new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            mod5Dao.UpdateUserBankDefault(MainBActivity.User.getUserId(),
                                                    carListAdapter.getData().get(position).getId());
                                        }
                                    }, null);
                            break;
                        case 1:
                            Tools.showNormalDialogOne(activity, "删除", "是否删除该银行卡"
                                    , new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            if (carListAdapter.getData().get(position).getIsDefault() == 1) {
                                                Toasty.normal(activity, "默认银行卡不可删除").show();
                                                return;
                                            }
                                            mod5Dao.DeleteUserBank(MainBActivity.User.getUserId(),
                                                    carListAdapter.getData().get(position).getId());
                                        }
                                    }, null);
                            break;
                        case 2:

                            break;
                    }

                }


            }
        };
        rvCar.setOnItemMenuClickListener(mItemMenuClickListener);

        carListAdapter = new BankCardListAdapter(null);
        rvCar.setAdapter(carListAdapter);

//        carListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Intent intent = new Intent(activity, AddCarActivity.class);
//                Bundle bundle = new Bundle();
//                bundle.putSerializable("data", carListAdapter.getData().get(position));
//                intent.putExtra("data", bundle);
//                startActivity(intent);
//            }
//        });

        rvCar.addItemDecoration(new SpacesItemDecoration(10));

        srlMain.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mod5Dao.SelectUserBankList(MainBActivity.User.getUserId(), page);
            }
        });
    }

    @Override
    public int setLayout() {
        return R.layout.activity_car_list;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

//    @Override
//    protected void onRestart() {
//        super.onRestart();
//        mod5Dao.SelectUserBankList(MainActivity.User.getUserId());
//    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 银行卡列表:
                SelectUserBankListBean bean = (SelectUserBankListBean) bindingViewBean.getBean();
                if (bean.getCode().equals("0000000")) {
                    carListAdapter.setNewData(bean.getData().getList());
                }
                if (srlMain != null) {
                    srlMain.setRefreshing(false);
                }
                break;

            case 删除银行卡:
                PubStatusBean statusBean = (PubStatusBean) bindingViewBean.getBean();
                if (statusBean.getCode().equals("0000000")) {
                    page = 1;
                    mod5Dao.SelectUserBankList(MainBActivity.User.getUserId(), page);
                    Toasty.success(activity, "删除成功").show();
                } else {
                    Toasty.error(activity, statusBean.getMsg()).show();
                }

                break;
            case 银行卡默认设置:
                PubStatusBean pubStatusBean = (PubStatusBean) bindingViewBean.getBean();
                if (pubStatusBean.getCode().equals("0000000")) {
                    page = 1;
                    mod5Dao.SelectUserBankList(MainBActivity.User.getUserId(), page);
//                    EventBus.getDefault().post(new EventBusMsgBean(默认爱车更换, ""));
                    Toasty.success(activity, "默认设置成功").show();
                } else {
                    Toasty.error(activity, pubStatusBean.getMsg()).show();
                }
                break;
        }
    }


    @OnClick({R.id.ll_back, R.id.ll_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.ll_right:
                Intent intent = new Intent(activity, AddBankCardActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void Event(EventBusMsgBean e) {
        switch (e.getmEnumStatus()) {
            case 银行卡添加:
                page = 1;
                mod5Dao.SelectUserBankList(MainBActivity.User.getUserId(), page);
                break;

        }
    }
}
